package com.zhangxi.config;

/**
 * ClassName: OssConfiguration
 * Package: com.zhangxi.config
 */

import com.zhangxi.properties.OssProperties;
import com.zhangxi.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置阿里云AliOssUtil工具类
 */
@Configuration
@Slf4j
public class OssConfiguration {

    /**
     * 需要上传时返回一个AliOssUtil的Bean
     * @param ossProperties
     * @return
     */
    @Bean
    @ConditionalOnMissingBean //仅在 Spring 上下文中不存在某个特定的 bean 时，才会创建标注了这个注解的 bean
    public AliOssUtil aliOssUtil(OssProperties ossProperties){
        log.info("开始创建阿里云文件上传工具类对象：{}",ossProperties);
        return new AliOssUtil(ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret(),
                ossProperties.getBucketName());
    }

}
